package com.neuedu.servlet;

import com.google.gson.Gson;

import com.neuedu.pojo.PageResult;
import com.neuedu.pojo.vo.FoodCategoryVO;
import com.neuedu.service.FoodCategoryService;
import com.neuedu.service.Imp.FoodCategoryServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/foodType")
public class FoodTypeServlet extends HttpServlet {
    private FoodCategoryService foodCategoryService = new FoodCategoryServiceImp();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String op = req.getParameter("op");
        switch (op) {
            case "get":
                getFoodType(req, resp);
                break;
            case "add":
                addFoodType(req, resp);
                break;
            case "update":
                updateFoodType(req, resp);
                break;
            case "del":
                delFoodType(req, resp);
                break;
            default:
                resp.getWriter().write(0+"");
                break;
        }
    }
    public void getFoodType(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        String shopId = req.getParameter("shopId");

        PageResult<List<FoodCategoryVO>> fc = foodCategoryService.getFoodCategory(currentPage, pageSize,shopId);

        Gson gson = new Gson();
        String json = gson.toJson(fc);

        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(json);
    }

    public void addFoodType(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String categoryName = req.getParameter("categoryName");
        String shopId = req.getParameter("shopId");

        Integer i = foodCategoryService.addFoodCategory(categoryName, shopId);
        resp.getWriter().write(i + "");
    }

    public void updateFoodType(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String categoryId = req.getParameter("categoryId");
        String categoryName = req.getParameter("categoryName");
        String shopId = req.getParameter("shopId");

        Integer i = foodCategoryService.updateFoodCategory(categoryId,categoryName,shopId);
        resp.getWriter().write(i + "");
    }

    public void delFoodType(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String categoryId = req.getParameter("categoryId");

        Integer i = foodCategoryService.delFoodCategory(categoryId);
        resp.getWriter().write(i + "");
    }
}
